package com.neusoft.aclome.cmp.cloud.server.jcce.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.aclome.cmp.cloud.server.jcce.dto.response.JcceJobDTO;
import com.neusoft.aclome.cmp.cloud.server.jcce.entity.JcceJobEntity;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.JcceJobMapper;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.JcceJobTaskMapper;
import com.neusoft.aclome.cmp.cloud.server.jcce.wrapper.JcceJobWrapper;
import com.neusoft.aclome.cmp.cloud.server.pub.cloud.entity.PubVirtualMachineEntity;
import com.neusoft.aclome.cmp.cloud.server.pub.cloud.repository.PubVirtualMachineMapper;
import com.neusoft.aclome.cmp.cloud.server.pub.cloud.service.PubVirtualMachineService;
import com.neusoft.aclome.cmp.cloud.server.service.VirtualMachineService;
import com.neusoft.aclome.cmp.common.exception.AclomeCMPException;
import com.neusoft.aclome.cmp.common.model.vo.HttpResponse;
import com.neusoft.aclome.cmp.common.model.vo.ResponsePage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * describe :
 *
 * @author hao.dd
 * @date 2023/11/6
 */
@Service
public class JcceJobService {

    private static final Logger log = LoggerFactory.getLogger(JcceJobService.class);

    @Autowired
    private PubVirtualMachineMapper pubVirtualMachineMapper;

    @Autowired
    private JcceJobMapper jcceJobMapper;

    @Autowired
    private JcceJobTaskMapper jcceJobTaskMapper;

    @Autowired
    private JobRunnerService jobRunnerService;

    @Autowired
    private JcceJobWrapper jcceJobWrapper;

    private static final String UPLOAD_DIR = "/opt/jcceJob/temp";

    public ResponsePage<JcceJobDTO> findJobsByCondition(String jobName, String jobType, Integer cloudPlatformId,
                                                        String status, Integer pageNum, Integer pageSize,
                                                        Date arriveStartTime,Date  arriveEndTime) {
        // 创建分页对象
        PageHelper.startPage(pageNum, pageSize);

        // 执行查询
//        List<JcceJobEntity> jobs = jcceJobMapper.selectByCondition(jobName, jobType, cloudPlatformId, status);
        List<JcceJobDTO> jobs = jcceJobMapper.selectByConditionToDto(jobName, jobType, cloudPlatformId, status ,arriveStartTime,arriveEndTime);

        // 封装为 PageInfo 对象
        PageInfo<JcceJobDTO> pageInfo = new PageInfo<>(jobs);
        ResponsePage<JcceJobDTO> jcceJobEntityResponsePage = new ResponsePage<JcceJobDTO>();
        jcceJobEntityResponsePage.setSize(pageInfo.getSize());
        jcceJobEntityResponsePage.setPages(pageInfo.getPages());
        jcceJobEntityResponsePage.setPage(pageNum);
        jcceJobEntityResponsePage.setTotal(pageInfo.getTotal());
        jcceJobEntityResponsePage.setPageData(pageInfo.getList());

        return jcceJobEntityResponsePage;
    }

    public HttpResponse createJcceJob(MultipartFile file, JcceJobDTO dto) {
        try {
            return HttpResponse.resultSuccess(jobRunnerService.run(file, dto));
        } catch (Exception e) {
            log.error("the create job method is error " + e.getMessage());
            return HttpResponse.resultError(500, e.getMessage());
        }
    }
}